<template>
  <div  class="app-container"> 
    <WarehousePropertyDetailEdit ref="openWarehousePropertyDetailEdit" @refresh="refresh"></WarehousePropertyDetailEdit>
    <WarehousePropertyMoveAdd ref="openWarehousePropertyMoveAdd" @refresh="refresh"></WarehousePropertyMoveAdd>
    <div class="detail-title-box">
      <span>详情信息</span>
    </div>
    <!-- 货权转移详情 -->
    <el-form ref="detailsQuery" :model="detailsQuery" label-width="100px" :inline="true">
      <el-form-item label="备注" prop="dzbz">
        <el-input
          v-model="detailsQuery.dzbz"
          placeholder="请输入备注"
          clearable
        />
      </el-form-item>
    </el-form>
    <!-- <div class="mt20 mb20 ml10">
      <el-descriptions>
        <el-descriptions-item label="锁仓线">{{ showData.scx }}</el-descriptions-item>
        <el-descriptions-item label="提示金额">{{ showData.tsje }}</el-descriptions-item>
        <el-descriptions-item label="所有货物剩余金额">{{ showData.hwje }}</el-descriptions-item>
      </el-descriptions>
    </div> -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handlePropertyMove" v-if="!getAuditState" v-hasPermi="['zhcloud:hqzy:add']">增加货物</el-button>
      </el-col>
    </el-row>
    <el-table
      :data="warehousePropertyMoveDetail"
      show-summary
      :summary-method="getSummaries"
      v-loading="loading"
    >
    <af-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="left">
      <template slot-scope="scope">
        <el-button
          :disabled="getAuditState"
          size="mini"
          type="text"
          @click="handleDelete(scope.row)"
          v-hasPermi="['zhcloud:hqzy:del']"
        >删除</el-button>
        <el-button
          :disabled="getAuditState"
          size="mini"
          type="text"
          @click="handleEdit(scope.row)"
          v-hasPermi="['zhcloud:hqzy:modify']"
        >修改</el-button>
      </template>
    </af-table-column>
      <af-table-column label="转移日期" prop="ssrqidn" />
      <af-table-column label="物品规格" prop="spzs" />
      <af-table-column label="批号" prop="spbh" />
      <af-table-column label="柜号" prop="spgh" />
      <af-table-column label="转移件数" prop="spjs" />
      <af-table-column label="单重(KG)" prop="spjsl" />
      <af-table-column label="转移吨数" prop="spsl" />
      <af-table-column label="货权费用交付" prop="hqf" />
      <af-table-column label="旧入仓日期" prop="ssrqid" />
      <af-table-column label="品牌" prop="spcd" />
      <af-table-column label="仓库" prop="spckmc" />
      <af-table-column label="仓位" prop="ckcw" />
      <af-table-column label="仓库条码" prop="sptmc" />
      <af-table-column label="旧用户条码" prop="sptm" />
      <af-table-column label="新用户条码" prop="sptmn" />
      <af-table-column label="旧所有人" prop="khmc" />
      <af-table-column label="新所有人" prop="khmcn" />
      <af-table-column label="备注" prop="itbz"  width="150" :show-overflow-tooltip="true"/>
    </el-table>
    <el-row type="flex" justify="center" :gutter="5" class="mt20">
      <el-col :span="1.5">
        <el-button type="primary" @click="handleWarehousePropertyReject" v-if="getAuditState" v-hasPermi="['zhcloud:hqzy:reject']">驳 回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" @click="handleWarehousePropertySubmit" v-if="!getAuditState" v-hasPermi="['zhcloud:hqzy:audit']">审 核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button  @click="handleWarehousePropertyEditRemark" v-hasPermi="['zhcloud:hqzy:modify']">保存</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button @click="goBack">返回</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>

import WarehousePropertyMoveAdd from './warehousePropertyMoveAdd.vue'
import WarehousePropertyDetailEdit from './warehousePropertyDetailEdit.vue'
import { getWarehousePropertyMoveDetail,warehousePropertyDelete, warehousePropertyAudit, warehousePropertyReject,warehousePropertyModifyRemark, warehousePropertyDetailRefresh } from "@/api/storage/commonGoods/warehouseProperty";
import { NUMBER_DIDGIT, WEIGHT_DIGIT } from '@/utils/constant'
import { calculateDays, formatDate } from '@/utils'

export default {
  name: "WarehousePropertyDetail",
  components: { WarehousePropertyDetailEdit, WarehousePropertyMoveAdd },
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //加载列表
      loading: true,
      // 详情信息
      detailsQuery: {
       	dzbz: "",
        dzid: "",
      },
      //获取详情参数
      queryParams:{
        dzid:"",
        shztn:""
      },
      //删除详情
      delQureyParams: {
        dzid: 0,
        prrq: "",
        spbhid: "",
        spckid: "",
        spcwid: "",
        spdzld: 0,
        spdzld2: 0
      },
      //显示数据
      // showData:{
      //   scx: 0,
      //   tsje: 0,
      //   hwje: 0
      // },
      //刷新参数
      // refreshQuery: {
      //   khid:""
      // },
      //审核入参
      auditQureyParams:[],
      //是否审核
      auditState:"",
      //驳回入参
      rejectQureyParams:{
        dzid:"",
      },
      //行数据
      warehousePropertyMoveDetail:[],
      //页数
      pageSize: 999,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  beforeRouteEnter(to, from, next) {
    const query = to.query
    next(vm => {   
      if(Object.keys(query).length > 0) {
        vm.reset(vm)
        vm.detailsQuery = query
        vm.queryParams.dzid =  query.dzid
        vm.queryParams.prrq = query.prrq
        vm.auditState = query.shzt
        vm.delQureyParams.dzid = query.dzid
        vm.rejectQureyParams.dzid = query.dzid
        //vm.refreshQuery.khid =  query.khmcid
        //vm.detailRefresh()
        vm.getList()
      }
    })
  },
  computed: {
    getAuditState() {
      return this.auditState  == "未审核"?false:true
    }
  },
  methods: {
    /**获取详情数据 */
    getList() {
      this.loading=true
      getWarehousePropertyMoveDetail(this.queryParams).then(response => {
        this.warehousePropertyMoveDetail = response.data;
        this.loading=false
      });
    },
    /**删除详情 */
    deleteWarehousePropertyDetail() {
      this.$modal.loading("数据正在删除中，请稍等...")
      warehousePropertyDelete(this.delQureyParams).then(response => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('删除成功！')
        this.getList()
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },

    /**审核 */
    auditWarehouseProperty() {
      this.$modal.confirm("是否确认次操作？").then(() => {
        this.$modal.loading("数据正在审核中，请稍等...")
        warehousePropertyAudit(this.auditQureyParams).then(response => {
          warehousePropertyModifyRemark(this.detailsQuery).then(response2 => {
            this.$modal.closeLoading()
            this.$modal.msgSuccess('审核通过！')
            this.goWarehouseProperty()
          }).catch(()=> {
            this.$modal.closeLoading()
          })
        }).catch(()=> {
          this.$modal.closeLoading()
        })
      }).catch(() => {})
    },

    /**驳回 */
    rejectWarehouseProperty() {
      this.$modal.confirm("是否确认次操作？").then(() => {
        this.$modal.loading("数据正在驳回中，请稍等...")
        warehousePropertyReject(this.rejectQureyParams).then(response => {
          this.$modal.closeLoading()
          this.$modal.msgSuccess('审核驳回！')
          this.goWarehouseProperty()
        }).catch(()=> {
          this.$modal.closeLoading()
        })
      }).catch(() => {})
    },
    /**新增货权转移单 */
    handlePropertyMove() {
      this.$refs.openWarehousePropertyMoveAdd.open(this.detailsQuery)
    },

    /**编辑行 */
    handleEdit(row) {
      this.$refs.openWarehousePropertyDetailEdit.open(row, this.queryParams.dzid)
    },
    /**删除行 */
    handleDelete(row) {
      this.$modal.confirm("是否删除记录？").then(() => {
      const {spbhidn:spbhid,spckid= "",spcwid= "",spdzld= 0,spdzld2= 0} = row
      this.delQureyParams = {...this.delQureyParams,spbhid,spckid,spcwid,spdzld,spdzld2}
      this.deleteWarehousePropertyDetail()
      }).catch(() => {})
    },
    /**修改备注 */
    handleWarehousePropertyEditRemark() {
      warehousePropertyModifyRemark(this.detailsQuery).then(response => {
        this.$modal.msgSuccess('修改备注成功！')
        this.goWarehouseProperty()
      });
    },
    /**提交审核 */
    handleWarehousePropertySubmit() {
      const nowDate = formatDate(Date.now()).split(" ")[0]
      const details = this.warehousePropertyMoveDetail
      if(this.auditState  == "已审核") return this.$modal.msgWarning("已审核的单不允许再次审核！")
      //如果当前时间（审核日期）小于转货日期，则系统提示：转货日期大于当前日期，审核失败！
      if(details && details.length > 0) {
        const sortDetails = details.sort((a,b) => a.ssrqidn>b.ssrqidn?-1:1)
        if(calculateDays(sortDetails[0].ssrqidn, nowDate) < 0) return this.$modal.msgWarning("转货日期大于当前日期，审核失败！")
      }


      this.auditQureyParams.push(this.detailsQuery.dzid)
      this.auditWarehouseProperty()
    },
    /** 驳回*/
    handleWarehousePropertyReject() {
      this.rejectWarehouseProperty()
    },
    //刷新数据
    // detailRefresh() {
    //   this.$modal.loading("数据正在刷新中，请稍等...")
    //   warehousePropertyDetailRefresh(this.refreshQuery).then(response => {
    //     this.$modal.closeLoading()
    //     const data = response.data
    //     this.showData = {
    //       scx :data.scx,
    //       tsje :data.tsje, 
    //       hwje :data.hwje
    //     }
    //   }).catch(()=> {
    //     this.$modal.closeLoading()
    //   })
    // },
    //合计
    getSummaries({ columns, data }) {
      const sums = []
      columns.forEach((column, index) => {
        if(index === 0) {
          sums[index] = "合计"
			    return
        }
        if ((column.property === "spjs" || column.property === "spsl")) {
          const values = data.map((item) => {
            return parseFloat(this.delcommafy(item[column.property]))
          });
          sums[index] = values.reduce((prev, curr) => {
            if(!prev) prev = 0
            if(!curr) curr = 0
             if(column.property === "spsl") {
              return parseFloat((prev + curr).toFixed(WEIGHT_DIGIT))
            }
             if(column.property === "spjs") {
              return parseFloat((prev + curr).toFixed(NUMBER_DIDGIT))
            }
          }, 0);
        }
      })
      return sums
    },
     /** 刷新数据*/
    refresh() {
      this.getList()
    },
   //跳回货物转移页
    goWarehouseProperty() {
      this.$tab.closePage(this.$route)
      this.$router.push({name:"WarehouseProperty", params:{refresh:true}})
    },
    reset(vm) {
      Object.assign(vm.$data, vm.$options.data())
    },
     /**返回不刷新 */
    goBack() {
      this.$router.push({name:"WarehouseProperty"})
    }
  }
};
</script>
